<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()"
             label-width="120px">
      <el-row>
        <el-col :span="24">
          <p class="ptitle-data-from">
            <img src="/form/jiben.png">
            <div style="width: 5px"></div>
            <span>基本信息</span>
          </p>
        </el-col>
        <el-col :span="24">
          <el-form-item style="width: 50%" label="所属杆塔" prop="stationId">
            <TowerElect v-model="dataForm.stationId" placeholder=""></TowerElect>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="相机名称" prop="name">
            <el-input v-model="dataForm.name" placeholder=""></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="相机类型" prop="type">
            <ren-select style="width: 100%" v-model="dataForm.type" dict-type="camera_type"
                        placeholder=""></ren-select>
          </el-form-item>
        </el-col>
        <!--        <el-col :span="12">-->
        <!--          <el-form-item label="nvrid" prop="nvrId">-->
        <!--            <el-input v-model="dataForm.nvrId" placeholder="nvrid"></el-input>-->
        <!--          </el-form-item>-->
        <!--        </el-col>-->
        <el-col :span="12">
          <el-form-item label="ip地址" prop="ip">
            <el-input v-model="dataForm.ip" placeholder=""></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="密码" prop="password">
            <el-input v-model="dataForm.password" placeholder=""></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备编号" prop="gbDeviceNo">
            <el-input v-model="dataForm.gbDeviceNo" placeholder=""></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="通道编号" prop="gbChannelNo">
            <el-input v-model="dataForm.gbChannelNo" placeholder=""></el-input>
          </el-form-item>
        </el-col>
        <!--        <el-col :span="12">-->
        <!--          <el-form-item label="是否在线" prop="isOnline">-->
        <!--            <el-input v-model="dataForm.isOnline" placeholder="是否在线" readonly></el-input>-->
        <!--          </el-form-item>-->
        <!--        </el-col>-->
        <el-col :span="24">
          <el-form-item label="国标地址" prop="gbFlv">
            <el-input v-model="dataForm.gbFlv" placeholder=""></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <!--      <el-form-item label="创建时间" prop="createDate">-->
      <!--        <el-input v-model="dataForm.createDate" placeholder="创建时间"></el-input>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="id" prop="id">-->
      <!--        <el-input v-model="dataForm.id" placeholder="id"></el-input>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="" prop="stationId">-->
      <!--        <el-input v-model="dataForm.stationId" placeholder=""></el-input>-->
      <!--      </el-form-item>-->
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import {reactive, ref} from "vue";
import baseService from "@/service/baseService";
import {useI18n} from "vue-i18n";
import {ElMessage} from "element-plus";
import TowerElect from "@/components/tower-elect.vue";


const {t} = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  createDate: "",
  id: "",
  stationId: "",
  type: "",
  name: "",
  ip: "",
  password: "",
  isOnline: "",
  gbFlv: "",
  nvrId: "",
  gbChannelNo: "",
  gbDeviceNo: ""
});

const rules = ref({});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/KxCamera/kxcamera/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/KxCamera/kxcamera", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
